package com.sudo.api._admin;

import com.sudo.common.annotation.ApiAuthorization;
import com.sudo.common.enums.SysStatus;
import com.sudo.common.utils.HttpUserInfoUtil;
import com.sudo.common.utils.JsonRes;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.dao.admin.entity.SysButton;
import com.sudo.dao.admin.entity.SysMenu;
import com.sudo.dao.admin.model.bo.*;
import com.sudo.dao.admin.model.vo.SysRoleButtonVO;
import com.sudo.starter.ratelimit.annotation.ApiIdempotent;
import com.sudo.service.admin.service.SysButtonService;
import com.sudo.service.admin.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 为道日损 2020-04-28 8:42
 */
@Api(value = "按钮接口",tags = {"按钮相关接口"})
@RestController
@RequestMapping("sysButton")
public class SysButtonController extends AdminBaseController {

    @Resource
    private SysButtonService sysButtonService;

    @Resource
    private SysMenuService sysMenuService;

    @ApiOperation(value = "根据菜单代码查找按钮",notes = "根据菜单代码查找按钮",httpMethod = "GET")
    @GetMapping("/getSysButtonByMenuCode")
    public JsonRes getSysButtonByMenuCode(SysButtonQueryBO sysButtonQueryBO) {
        PagedGridResult sysButtonList = sysButtonService.getSysButtonByMenuCode(sysButtonQueryBO);
        return JsonRes.ok(sysButtonList);
    }

    @ApiOperation(value = "根据buttonCode获取按钮",notes = "根据buttonCode获取按钮",httpMethod = "GET")
    @GetMapping("/getSysButtonByMenuButtonCode")
    public JsonRes getSysButtonByMenuButtonCode(SysButtonOneBO sysButtonOneBO) {

        SysButton sysButton = sysButtonService.getSysButtonByMenuButtonCode(sysButtonOneBO.getMenuCode(),sysButtonOneBO.getButtonCode());
        return JsonRes.ok(sysButton);
    }

    @ApiIdempotent
    @ApiAuthorization(menu = "btnConf",button = "update")
    @ApiOperation(value = "根据menuButtonCode修改按钮",notes = "根据menuButtonCode修改按钮",httpMethod = "POST")
    @PostMapping("/updateSysButtonByMenuButtonCode")
    public JsonRes updateSysButtonByMenuButtonCode(@RequestBody @Validated SysButtonUpdateBO sysButtonUpdateBO,
                                       HttpServletRequest request) {
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("仅有超级用户能执行该操作");
        }
        if (undelMenu(sysButtonUpdateBO.getMenuCode())) {
            return JsonRes.errorMsg("系统固有菜单下的按钮禁止修改");
        }
        int affectCount = sysButtonService.updateSysButtonByMenuButtonCode(sysButtonUpdateBO);

        return JsonRes.judge(affectCount,"修改按钮成功","修改按钮失败");
    }

    @ApiIdempotent
    @ApiAuthorization(menu = "btnConf",button = "add")
    @ApiOperation(value = "新增按钮",notes = "新增按钮",httpMethod = "POST")
    @PostMapping("/addSysButton")
    public JsonRes addSysButton(@RequestBody @Validated SysButtonAddBO sysButtonAddBO,
                                       HttpServletRequest request) {
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("仅有超级用户能执行该操作");
        }
        if (undelMenu(sysButtonAddBO.getMenuCode())) {
            return JsonRes.errorMsg("禁止给固有菜单添加按钮");
        }
        //判断菜单是否存在
        SysMenu sysMenu = sysMenuService.getSysMenuByCode(sysButtonAddBO.getMenuCode());
        if (null == sysMenu || SysStatus.OFF.name().equals(sysMenu.getStatus())) {
            return JsonRes.errorMsg("当前菜单不可用");
        }

        //判断按钮code是否已存在
        boolean flag = sysButtonService.isButtonCodeExist(sysButtonAddBO.getMenuCode(),sysButtonAddBO.getButtonCode());
        if (flag) {
            return JsonRes.errorMsg("代码已存在，请重新编辑");
        }

        int affectCount = sysButtonService.addSysButton(sysButtonAddBO);
        return JsonRes.judge(affectCount,"新增按钮成功","新增按钮失败");
    }

    @ApiIdempotent
    @ApiAuthorization(menu = "btnConf",button = "delete")
    @ApiOperation(value = "删除按钮",notes = "删除按钮",httpMethod = "POST")
    @PostMapping("/deleteSysButtonByMenuButtonCode")
    public JsonRes deleteSysButtonByMenuButtonCode(
            @RequestBody @Validated SysButtonDeleteBO sysButtonDeleteBO,
            HttpServletRequest request) {
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("仅有超级用户能执行该操作");
        }
        if (undelMenu(sysButtonDeleteBO.getMenuCode())) {
            return JsonRes.errorMsg("系统固有菜单下的按钮禁止删除");
        }
        int affectCount = sysButtonService.deleteSysButtonByMenuButtonCode(sysButtonDeleteBO);
        return JsonRes.judge(affectCount,"删除按钮成功","删除按钮失败");
    }

    @ApiOperation(value = "根据roleCode获取对应菜单按钮权限",notes = "根据roleCode获取对应菜单按钮权限",httpMethod = "GET")
    @GetMapping("getButtonCodeByRoleMenuCode")
    public JsonRes getButtonCodeByRoleMenuCode(@RequestParam(required = false) String roleCode,
                                               @RequestParam String menuCode) {

        List<SysRoleButtonVO> buttonCodeByRoleMenuCode = sysButtonService.getButtonCodeByRoleMenuCode(roleCode, menuCode);

        return JsonRes.ok(buttonCodeByRoleMenuCode);
    }
}
